package com.yanqu.road.server.gameplayer.module.activity.horserun.event;

import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;
import com.yanqu.road.entity.activity.horserun.config.HorseRunEventConfig;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.manger.activity.horserun.HorseRunMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *  exp("经验事件 遇到直接获得完成",2),
 */
public class HorseRunExpAddEvent extends HorseRunBaseEvent{

    @Override
    public HorseRunProto.EventResult.Builder meet(HorseRunUserGamingData userGamingData, HorseRunConfig config,HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();

        builder.setError(0);

        int addExp = eventConfigById.getParamList().get(3);

        Random random = HorseRunMgr.getRandom();

        int minFloat = eventConfigById.getParamList().get(0);
        int maxFloat = eventConfigById.getParamList().get(1);

        int floatRate = 1000;
        if (minFloat == maxFloat) {
            floatRate = minFloat;
        }else if (minFloat < maxFloat){
            floatRate = random.nextInt(maxFloat-minFloat)+minFloat;
        }
        addExp = BigDecimal.valueOf((long) addExp * floatRate).divide(BigDecimal.valueOf(1000),0, RoundingMode.DOWN).intValue();

        addExp = addExp * userGamingData.getOpenGameMultiple();


        builder.setExpAdd(addExp);
        userGamingData.addExpSum(addExp);

        List<Integer> logParam = new ArrayList<>();
        logParam.add(addExp);

        addUserLog(userGamingData,config,1,eventId,logParam,builder);
        eventDone(userGamingData);
        return builder;
    }



}
